<?php
/**
 * 会员站外文章控制器
 */
namespace Admin\Controller;
use Think\Controller;
class ArticleController extends InitController {
    
	// 文章列表
	public function articleList() {
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		
		$articleModel = D('Article');
		$title = I('title');
		$start_time = strtotime(I('start_time'));
		$end_time = strtotime(I('end_time'));

		if($title){
			$where['s.title'] = array("like",'%'.$title.'%');
		}
		
		//去除-1
		$where['s.status']   = array('NEQ','-1');
		
		// 文章状态
		$stateWait = I('stateWait',0,'intval'); // 待审核
		$statePass = I('statePass',0,'intval'); // 已通过
		$stateVeto = I('stateVeto',0,'intval'); // 被否决
		if($stateWait || $statePass || $stateVeto)
		{
			$stateWhere = array();
			if ($stateWait) {
				$stateWhere[] = '0';
			}
			if ($statePass) {
				$stateWhere[] = '1';
			}
			if ($stateVeto) {
				$stateWhere[] = '-2';
			}
			$where['s.status'] = array('in',$stateWhere);
		}
		else
		{
			// 无选择时即全选
			$stateWait = 1;
			$statePass = 1;
			$stateVeto = 1;
		}
		
		if (!empty($start_time) && !empty($end_time)) {
			// 如果有开始及结束时间 则取中值
			$where['s.createtime'] = array(array('EGT', $start_time), array('ELT', $end_time));
		}else{
			if (!empty($start_time)) {
				// 有开始时间 
				$where['s.createtime'] = array('EGT', $start_time);
			}elseif (!empty($end_time)) {
				// 有结束时间 
				$where['s.createtime'] = array('ELT', $end_time);
			}
		}

		$sort = I('sort',1,'intval');
		$sortList = array(
			'1' => array('name' => '时间↓','sql' => 's.createtime desc'),
			'2' => array('name' => '时间↑','sql' => 's.createtime asc'),
		);
		$order = $sortList[$sort]['sql'];

		$total = $articleModel->alias('s')->where($where)->count('id');
		
		$Page = $this->setPageHander($total);
		$show = $Page?$Page->show():'';

		$list = $articleModel->getArticleList($where,$order,$Page->firstRow.','.$Page->listRows,1);

		$this->assign('list',$list);
		$this->assign('pager',$show);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->assign('start_time',$start_time);
		$this->assign('end_time',$end_time);
		$this->assign('title',$title);
		$this->assign(array(
			'stateWait' => $stateWait,
			'statePass' => $statePass,
			'stateVeto' => $stateVeto,
		));
		$this->assign(array(
			'sortList' => $sortList,
			'sort' => $sort,
		));
		$this->display('Article/articleChecklist');
	}

	// 批量通过
	public function articleBatchPass()
	{
		if (!IS_AJAX)
		{
			$this->ajaxReturn(array('error' => 1, 'errmsg' => '非法操作！', 'field' => ''));
		}
		$batchId = I('batchId',array());
		if (empty($batchId)) {
			$this->ajaxReturn(array('error' => 1, 'errmsg' => '参数错误！', 'field' => ''));
		}
			// $this->ajaxReturn(var_export($batchId,true));
		$where = array('id' => array('in',array_values($batchId)));
			// $this->ajaxReturn(var_export($where,true));
		$result = M('Article')->where($where)->save(array('status' => 1));
			// $this->ajaxReturn(var_export($result,true));
		if ($result !== false) {
			$this->ajaxReturn(array('error' => 0, 'errmsg' => $result.' 篇文章[通过]成功！', 'field' => ''));
		} else {
			$this->ajaxReturn(array('error' => 1, 'errmsg' => '操作失败！', 'field' => ''));
		}
	}

	// 批量否决
	public function articleBatchVeto()
	{
		if (!IS_AJAX)
		{
			$this->ajaxReturn(array('error' => 1, 'errmsg' => '非法操作！', 'field' => ''));
		}
		$batchId = I('batchId',array());
		if (empty($batchId)) {
			$this->ajaxReturn(array('error' => 1, 'errmsg' => '参数错误！', 'field' => ''));
		}
			// $this->ajaxReturn(var_export($batchId,true));
		$where = array('id' => array('in',array_values($batchId)));
			// $this->ajaxReturn(var_export($where,true));
		$result = M('Article')->where($where)->save(array('status' => -2));
			// $this->ajaxReturn(var_export($result,true));
		if ($result !== false) {
			$this->ajaxReturn(array('error' => 0, 'errmsg' => $result.' 篇文章[否决]成功！', 'field' => ''));
		} else {
			$this->ajaxReturn(array('error' => 1, 'errmsg' => '操作失败！', 'field' => ''));
		}
	}

	// 批量删除
	public function articleBatchDel()
	{
		if (!IS_AJAX)
		{
			$this->ajaxReturn(array('error' => 1, 'errmsg' => '非法操作！', 'field' => ''));
		}
		$batchId = I('batchId',array());
		if (empty($batchId)) {
			$this->ajaxReturn(array('error' => 1, 'errmsg' => '参数错误！', 'field' => ''));
		}
			// $this->ajaxReturn(var_export($batchId,true));
		$where = array('id' => array('in',array_values($batchId)));
			// $this->ajaxReturn(var_export($where,true));
		$result = M('Article')->where($where)->save(array('status' => -1));
			// $this->ajaxReturn(var_export($result,true));
		if ($result !== false) {
			$this->ajaxReturn(array('error' => 0, 'errmsg' => $result.' 篇文章[删除]成功！', 'field' => ''));
		} else {
			$this->ajaxReturn(array('error' => 1, 'errmsg' => '操作失败！', 'field' => ''));
		}
	}
	
	// 删除文章
	public function articleDel() {
		if (!IS_AJAX) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'非法操作！','field'=>''));
		}
		$id = I('id',0,'intval');
		if (!$id) {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'参数错误！','field'=>''));
		}
		if (M('Article')->where(array('id'=>$id))->save(array('status'=>-1)) !== false) {
			$this->ajaxReturn(array('error'=>0,'errmsg'=>'操作成功！','field'=>''));
		} else {
			$this->ajaxReturn(array('error'=>1,'errmsg'=>'操作失败！','field'=>''));
		}
	}
	
	// 审核文章
	public function articleEdit() {
		$id = I('id',0,'intval');
		if (!$id) {
			$this->error('参数错误');
		}
		$topmenu = $this->topmenu;
		$sidebarmenu = $this->sidebarmenu[I('get.k')];
		$p  = I('p',0,'intval');
        $id = I('id',0,'intval');
        $articleModel = D('Article');
        $data = $articleModel->getArticleById($id);	
        
		$this->assign('p',$p);
        $this->assign('info',$data);
		$this->assign('topmenu',$topmenu);
		$this->assign('sidebarmenu',$sidebarmenu);
		$this->display('Article/articleCheckedit');
	}
	
    //审核文章显示内容
    public function article_review_display(){
        $id = I('id',0,'intval');
        $articleModel = D('Article');
        $data = $articleModel->getArticleById($id);	
        
		//替换链接，和position事件
		$data['content'] = str_replace('href','data-href',$data['content']);
		$data['content'] = str_replace('position(this)',' ',$data['content']);
        
		$this->assign('info',$data);
        $this->display('Article/article_review_display');
    }


	// 审核会员文章外部异步
	public function articleCheckpass(){
		if(!IS_AJAX){
			return false;
		}
		$id = I('id',0,'intval');
		$p = I('p',0,'intval');
		$status = I('status',0,'intval');
		
		if(empty($id)){
			$this->ajaxReturn(array('error'=>'-1','errmsg'=>'操作失败','field'=>''));
		}
		$info = D('Article')->changeStatus($id,$status);

		if($info){
			$this->ajaxReturn(array('error'=>'0','errmsg'=>'操作成功','field'=>'','url'=>U('Admin/Article/articleList',array('k'=>2,'sk'=>1,'p'=>$p))));
		}else{
			$this->ajaxReturn(array('error'=>'-1','errmsg'=>'操作失败','field'=>''));
		}
	}
	
}